// pages/cart/cart.js
import {Cart} from "cart-model.js";

var cart = new Cart();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(){
    var cartProduct =  cart.getCartDataFromLocal(); 
    var counts = cart.getCartTotalCounts(true);
    var result = this._getOrderTotalMoney(cartProduct);
    this.data.cartData = cartProduct;
    this.data.isSelectedAll = this._isProductSelectAll(this.data.cartData);
    // console.log(this.data.cartData);
    this.setData({
      "cartData":this.data.cartData,
      "selectedTypeCounts":result.selectedTypeCounts,
      "selectedCounts":result.selectedCounts,
      "account":result.account,
      "selectAll":this.data.isSelectedAll
    });
  },


  _getOrderTotalMoney(data){
    let len = data.length;
    // 总价格
    var account = 0; 
    var selectedCounts = 0;
    var selectedTypeCounts = 0;
    var multiple = 100;

    for(let i = 0; i < len; i++){
      if(data[i].selectStatus){
          account += data[i].counts * multiple * data[i].price * multiple;
          selectedCounts += data[i].counts;
          selectedTypeCounts ++;
      }
    }
    return {
        account:account / (multiple * multiple),
        selectedCounts:selectedCounts,
        selectedTypeCounts:selectedTypeCounts
    }
 },

//  单选事件
  toggleSelect:function(event){
    var id = cart.getDataSet(event,'id');
    var status = cart.getDataSet(event,'status');
    var index = this._isProductIdGetIndex(id);
    // console.log(index);
    this.data.cartData[index].selectStatus = !status;
    this._restCartData(this.data.cartData);  
  },

  // 重新计算购物车的总数量和总价格
  _restCartData:function(data){
    var newData = this._getOrderTotalMoney(data);
    this.data.isSelectedAll = this._isProductSelectAll(data);
    // console.log(this.data.isSelectedAll);    
    this.setData({
      "cartData":this.data.cartData,
      "selectedTypeCounts":newData.selectedTypeCounts,
      "selectedCounts":newData.selectedCounts,
      "account":newData.account,
      "selectAll":this.data.isSelectedAll
    });
  },

  // 全选事件
  toggleSelectAll:function(event){
    this.data.isSelectedAll = this._isProductSelectAll(this.data.cartData);
    if(this.data.isSelectedAll){
      this._setCartSelectedAll(false);
    }else{
      this._setCartSelectedAll(true);
    }
    this._restCartData(this.data.cartData);  
  },

  // 根据商品ID获取对应的下标值
  _isProductIdGetIndex:function(id){
    var data = this.data.cartData
    var len = data.length;
    for(let i = 0; i < len; i++){
      if(id == data[i].id){
        return i;
      }
    } 
  },

  // 判断购物车商品是否是全选状态
  _isProductSelectAll:function(data){
    var len = data.length;
    for(let i = 0; i < len; i++){
      if(data[i].selectStatus == false){
        return false;
      }
    }
    return true;
  },

  // 设置购物车商品全选或全不选
  _setCartSelectedAll:function(flag){
    var len = this.data.cartData.length;
    for(let i = 0; i < len; i++){
      if(flag){
        this.data.cartData[i].selectStatus = true;
      }else{
        this.data.cartData[i].selectStatus = false;
      }
    }
    console.log(this.data.cartData);
    this._restCartData(this.data.cartData);
  },

  // 更新购物车单个商品数量
  changeCounts:function(event){
    var id = cart.getDataSet(event,'id'),
    type = cart.getDataSet(event,'type'),
    index = this._isProductIdGetIndex(id);
    // console.log(type);
    if(type == "add"){
      this.data.cartData[index].counts++;
    }else{
      this.data.cartData[index].counts--;
    }
    if(this.data.cartData[index].counts < 1){
      this.data.cartData[index].counts = 1;
      return;
    }
    this._restCartData(this.data.cartData);
   
  },

  // 删除购物车的单个商品
  deleteCartProduct:function(event){
    var id = cart.getDataSet(event,'id'),
    index = this._isProductIdGetIndex(id);
    this.data.cartData.splice(index,1);
    this._restCartData(this.data.cartData);
  },
  
  // 提交购物车的商品,进入订单详情页面
  submitOrder:function(event){
    // console.log(this.data.cartData); 
    wx.navigateTo({
      url: '../order/order?account=' + this.data.account + '&from=cart'  
    });
  },

   /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    cart.execSetStorageSync(this.data.cartData);  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})